package com.heima.search.service.impl;

import com.heima.common.dto.ResponseResult;
import com.heima.common.dto.User;
import com.heima.common.util.AppThreadLocalUtil;
import com.heima.search.dto.ApArticleSearchDto;
import com.heima.search.entity.ApUserSearch;
import com.heima.search.service.IUserSearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.CriteriaDefinition;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class UserSearchServiceImpl implements IUserSearchService {


    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    @Async
    public void saveRecord(String keyword, Integer userId, String equipmentId) {
        System.out.println(Thread.currentThread().getName() + "开始执行异步任务..." + new Date());
        // 判断当前的实体(用户,设备) 和当前的关键字是否已经存在
        Query query = new Query();
        Criteria criteria;
        if (userId != null) {
            criteria = Criteria.where("userId").is(userId);
        } else {
            criteria = Criteria.where("equipmentId").is(equipmentId);
        }
        query.addCriteria(criteria);
        query.addCriteria(Criteria.where("keyword").is(keyword));
        long count = mongoTemplate.count(query, ApUserSearch.class);
        if (count > 0) return;
        // 保存用户的搜索记录到MongoDB
        ApUserSearch userSearch = new ApUserSearch();
        if (userId != null) {
            userSearch.setUserId(userId);
        } else {
            userSearch.setEquipmentId(equipmentId);
        }
        userSearch.setKeyword(keyword);
        userSearch.setCreatedTime(new Date());
        mongoTemplate.save(userSearch);
    }

    @Override
    public ResponseResult load(ApArticleSearchDto dto) {
        // 根据当前的用户或者设备
        // 默认加载5条数据
        // 按照创建时间倒序排列
        Query query = new Query();
        Criteria criteria;
        User user = AppThreadLocalUtil.get();
        if (user != null) {
            criteria = Criteria.where("userId").is(user.getUserId());
        } else {
            criteria = Criteria.where("equipmentId").is(dto.getEquipmentId());
        }
        query.addCriteria(criteria);
        Pageable page = PageRequest.of(0, dto.getSize(), Sort.Direction.DESC, "createdTime");
        query.with(page);
        List<ApUserSearch> list = mongoTemplate.find(query, ApUserSearch.class);
        return ResponseResult.okResult(list);
    }

    @Override
    public ResponseResult del(ApArticleSearchDto dto) {
        // 获取集合对象
        List<ApUserSearch> hisList = dto.getHisList();
        List<String> ids = new ArrayList<>();
        for (ApUserSearch userSearch : hisList) {
            ids.add(userSearch.getId());
        }
        Query query = new Query();
        query.addCriteria(Criteria.where("id").in(ids));
        mongoTemplate.remove(query, ApUserSearch.class);
        return ResponseResult.okResult();
    }
}
